package cn.school.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "IpFilter",value = "/*")
public class IpFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        chain.doFilter(req, resp);//放行

//        //System.nanoTime();
//
//        HttpServletRequest request=(HttpServletRequest)req;
//        System.out.println("ip:"+request.getRemoteHost()+",当前访问的地址："+request.getServletPath());
//        String path=request.getServletPath();
//        if(path.startsWith("/doLogin.jsp")){
//            chain.doFilter(req, resp);//放行
//            return;
//        }
//        if(request.getSession().getAttribute("USER_LOGIN")==null){
//            request.setAttribute("msg","您没有登录，不能访问");
//            request.getRequestDispatcher("/login.jsp").forward(request,resp);
//            return;
//        }
//        //        if(request.getRemoteHost().equals("192.168.1.143")){
////            HttpServletResponse re= (HttpServletResponse) resp;
////            re.sendError(405,"您没有权限访问");
////            return;
////        }
//        chain.doFilter(req, resp);//放行
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
